<?php

namespace App\Models;

use App\ModelFilters\Qh_OrderFilter;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Qh_Order extends BaseModel
{
    protected $table = 'tiku_order';

    protected $guarded = [];

    protected $appends = ['resourceRelation', 'typeText', 'statusText'];

    public function modelFilter(): ?string
    {
        return $this->provideFilter(Qh_OrderFilter::class);
    }

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'userId');
    }

    public function getResourceRelationAttribute()
    {
        if (request()->is('admin/*')) {
            if ($this->type == 1) return Qh_Course::find($this->relationId);
            if ($this->type == 2) return Qh_Cdkey::find($this->relationId);
            if ($this->type == 3) return LearnVideo::find($this->relationId);
            if ($this->type == 4) return CourseColumn::find($this->relationId);
        }
        return null;
    }

    public function getTypeTextAttribute()
    {
        switch ($this->type) {
            case 1:
                return '购买题库';
            case 2:
                return '购买激活码';
            case 3:
                return '购买视频课程';
            case 4:
                return '购买专栏';
            default:
                return '其他';
        }
    }

    public function getStatusTextAttribute()
    {
        switch ($this->type) {
            case 1:
                return '未支付';
            case 2:
                return '已支付';
            case 3:
                return '手动支付';
            case 4:
                return '已完成';
            case 8:
                return '资源发放失败';
            case 9:
                return '已退款';
            default:
                return '未知';
        }
    }
}
